//本题由于数量级比较小, 所以使用karaskal算法
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let cityNum, n, mustNum;
let initArr = [];
let mustArr = [];
rl.on("line", (line) => {
    if (cityNum === undefined) {
        [cityNum, n, mustNum] = line.trim().split(" ").map(Number)
    } else if (initArr.length < n) {
        let temp = line.trim().split(" ").map(Number)
        // 城市编号排序
        if (temp[0] > temp[1]) {
            let num = temp[0]
            temp[0] = temp[1]
            temp[1] = num
        }
        initArr.push(temp)
        if (initArr.length === n && mustNum === 0) {
            console.log(getResult(cityNum, n, mustNum, initArr, mustArr))
            // clear
            cityNum = n = mustNum = undefined
            initArr.length = 0
        }
    } else {
        let temp = line.trim().split(" ").map(Number)
        // 城市编号排序
        if (temp[0] > temp[1]) {
            let num = temp[0]
            temp[0] = temp[1]
            temp[1] = num
        }
        mustArr.push(temp)
        if (mustArr.length === mustNum) {
            console.log(getResult(cityNum, n, mustNum, initArr, mustArr))
            // clear
            cityNum = n = mustNum = undefined
            initArr.length = 0
        }
    }
})

/**
 *
 * @param cityNum {number}
 * @param n {number}
 * @param mustNum {number}
 * @param initArr {[[a, b, c]]}
 * @param mustArr {[[a, b]]}
 */
function getResult(cityNum, n, mustNum, initArr, mustArr) {
    // 检查是否能组成全网
    let set = new Set()
    for (const [a, b, c] of initArr) {
        set.add(a)
        set.add(b)
    }
    if (set.size < cityNum) {
        return -1
    }

    let ans = 0
    let visited = new Set()
    if (mustNum > 0) {
        for (const [a, b] of mustArr) {
            visited.add(a)
            visited.add(b)
            for (let i in initArr) {
                const [A, B, C] = initArr[i]
                if (a === A && b === B) {
                    ans += C
                    break
                }
            }
        }
    }

    if (visited.size === cityNum) return ans;

// 领接表
    let neighbor = {} // {key: [[node, weight]]}
    for (const [a, b, c] of initArr) {
        if (!neighbor[a]) {
            neighbor[a] = []
        }
        if (!neighbor[b]) {
            neighbor[b] = []
        }
        neighbor[a].push([b, c])
        neighbor[b].push([a, c])
    }

    let pq = [] // 初始化, 选第一个
    if (mustNum === 0) {
        pq.push([1, 0])
    } else {
        // 把已访问的,全都放入
        for (const num of visited.values()) {
            if (neighbor[num]) {
                pq.push(...neighbor[num])
            }
        }
        pq.sort((a, b) => a[1] - b[1])
    }

    while (visited.size < cityNum) {
        const [node, weight] = pq.shift()
        if (visited.has(node)) continue;

        visited.add(node)
        ans += weight

        for (const [a, b] of neighbor[node]) {
            if (!visited.has(a)) {
                pq.push([a, b])
            }
        }
        // 权值升序
        pq.sort((a, b) => a[1] - b[1])
    }
    return ans
}
